SET QUOTED_IDENTIFIER OFF;
SET ANSI_NULLS ON;
SET NOCOUNT ON;
GO

IF EXISTS (	SELECT ROUTINE_TYPE, *
		FROM INFORMATION_SCHEMA.ROUTINES
		WHERE ROUTINE_NAME = 'SplitString'
			AND ROUTINE_TYPE = 'FUNCTION' )
BEGIN
	PRINT 'Dropping Function dbo.SplitString.';
	DROP FUNCTION [dbo].[SplitString];
END

GO
-------------------------------------------------------------------------------
--	METADATA AND OTHER MESSAGES ABOVE.
--	BEGIN CREATE STATEMENTS.
-------------------------------------------------------------------------------

CREATE FUNCTION [dbo].[SplitString] (@String varchar(MAX), @Delimiter char(1))       
	returns @temptable TABLE (items varchar(MAX))       
	as       
	begin      
		declare @idx int       
		declare @slice varchar(8000)       

		select @idx = 1       
			if len(@String)<1 or @String is null  return       

		while @idx!= 0       
		begin       
			set @idx = charindex(@Delimiter,@String)       
			if @idx!=0       
				set @slice = left(@String,@idx - 1)       
			else       
				set @slice = @String       

			if(len(@slice)>0)  
				insert into @temptable(Items) values(@slice)       

			set @String = right(@String,len(@String) - @idx)       
			if len(@String) = 0 break       
		end   
	return 
	end
GO

-------------------------------------------------------------------------------
--	END OF CREATE STATEMENTS.
--	METADATA AND SUCCESS/ERROR MESSAGES BELOW.
-------------------------------------------------------------------------------
IF EXISTS (	SELECT *
			FROM INFORMATION_SCHEMA.ROUTINES
			WHERE ROUTINE_NAME = 'SplitString'
				AND ROUTINE_TYPE = 'FUNCTION' )
BEGIN
	PRINT 'Function dbo.SplitString created successfully.';	
END
ELSE
BEGIN
	PRINT 'ERROR: Failed to create Function dbo.SplitString.';
END
GO



